package com.sskj.level.presenter;

import android.text.TextUtils;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.lib.bean.PageBean;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.CurrentHolderFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class CurrentHolderFragmentPresenter extends BasePresenter<CurrentHolderFragment> {

    public void getTradeList(String type, String code) {
        if (TextUtils.isEmpty(SPUtil.get(SPConfig.ID, ""))) {
            mView.showEmpty();
            return;
        }
        httpService.getTradeList(type, code)
                .tag(this)
                .execute(new JsonCallBack<HttpData<PageBean<TradeListBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<TradeListBean>>> response) {
                        HttpData<PageBean<TradeListBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getList());
                        }
                    }
                });

    }
    public Flowable<List<TradeListBean>> getTradeListFlow(String type, String code) {
        if (TextUtils.isEmpty(SPUtil.get(SPConfig.ID, ""))) {
            return Flowable.fromIterable(new ArrayList<>());
        }
         return httpService.getTradeList(type, code)
                .converter(new JsonConverter<HttpData<PageBean<TradeListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());


    }

    public void pingCang(String orderId) {
        httpService.pingCang(orderId)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.pingCangSuccess();
                        }
                    }
                });
    }

    public void setStop(String orderId, String stopProfitPrice, String stopLossPrice) {
        httpService.setStop(orderId, stopProfitPrice, stopLossPrice)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }

    public void getProduct(String code) {
        httpService.getProduct(code)
                .execute(new JsonCallBack<HttpData<List<StockCoinBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<StockCoinBean>>> response) {
                        HttpData<List<StockCoinBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK && httpData.getData().size() == 1) {
                            mView.setStop(httpData.getData().get(0));
                        }
                    }
                });
    }
}
